package com.parking.user.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.parking.common.core.model.Result;
import com.parking.common.core.model.StateData;
import com.parking.dto.UserCarDtos;
import com.parking.exception.UserException;
import com.parking.exception.UserExceptionCode;
import com.parking.form.AddUserCarForm;
import com.parking.form.UpUserCarForm;
import com.parking.user.client.dto.UserOrderDto;
import com.parking.user.client.inter.UserOrderClient;
import com.parking.user.dao.UserAccountMapper;
import com.parking.user.dao.UserInfoMapper;
import com.parking.user.entity.UserAccount;
import com.parking.user.entity.UserCar;
import com.parking.user.dao.UserCarMapper;
import com.parking.user.entity.UserInfo;
import com.parking.user.service.UserCarService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 香菜狗带
 * @since 2023年11月28日
 */
@Service
public class UserCarServiceImpl extends ServiceImpl<UserCarMapper, UserCar> implements UserCarService {
    @Resource
    private UserCarMapper userCarMapper;
    @Resource
    private UserAccountMapper userAccountMapper;
    @Resource
    private UserInfoMapper userInfoMapper;
    @Resource
    private RabbitTemplate rabbitTemplate;
    @Resource
    private UserOrderClient orderClient;



    /**
     * 判断用户车牌号是否正确
     *  phone:用户电话
     * userRealName:用户姓名
     * userCarBrand:用户车牌号
     * */
//    public Boolean userCarBrand(String phone,String realName,String userBrand,Integer userId) {
//        QueryWrapper<UserCar> wrapper = new QueryWrapper<>();
//        wrapper.eq("car_contracts_tek",phone);
//        wrapper.eq("car_contacts",realName);
//        UserCar userCar = userCarMapper.selectOne(wrapper);
//        if (userCar!=null) {
//            throw new UserException(UserExceptionCode.USERINFO_IS_PHONE);
//        } else {
//            QueryWrapper<UserInfo> wrapper2 = new QueryWrapper<>();
//            wrapper2.eq("user_account_id",userId);
//            userInfoMapper.selectOne(wrapper2);
//            QueryWrapper<UserCar> wrapper2 = new QueryWrapper<>();
//            wrapper2.eq("user_car_brand",userBrand);
//            UserCar userCar2 = userCarMapper.selectOne(wrapper2);
//            if (userCar2==null) {
//                return true;
//            }
//        }
//        return false;
//    }

    @Override
    public void addUserCar(AddUserCarForm form, Integer userId) {
        QueryWrapper<UserCar> carwrapper = new QueryWrapper<>();
        carwrapper.eq("user_car_plate",form.getUserCarPlate());
        UserCar userCar=userCarMapper.selectOne(carwrapper);
        if (userCar!=null) {
            throw new UserException(UserExceptionCode.USER_CAR_INFO_HAVE);
        }
        //查询有没有相同车辆信息
        QueryWrapper<UserCar> carwrapper2 = new QueryWrapper<>();
        carwrapper.eq("car_contracts_tek",form.getCarContractsTek());
        UserCar userCar1=userCarMapper.selectOne(carwrapper);
        if (userCar1!=null) {
            throw new UserException(UserExceptionCode.USER_CAR_INFO_HAVE);
        }
        //判断用户账号是否正常使用
        UserAccount account = userAccountMapper.selectById(userId);
        if (account==null) {
            throw new UserException(UserExceptionCode.USERINFO_IS_NOT);
        }
        if (account.getUserState()!= StateData.USER_STATE_ENABLE) {
            throw new UserException(UserExceptionCode.USER_ACCOUNT_STATE_ISNOT);
        }
        //判断是否实名
        QueryWrapper<UserInfo> wrapper = new QueryWrapper<UserInfo>();
        wrapper.eq("user_account_id",userId);
        UserInfo userInfo = userInfoMapper.selectOne(wrapper);
        if (userInfo.getUserRealNameState()!= StateData.USER_REALNAME_STATE) {
            throw new UserException(UserExceptionCode.USER_REAL_NAME_STATE_NO);
        }
        //判断用户车俩信息是否一致
//        if (!form.getCarContacts().equals(userInfo.getUserRealName())) {
//            throw new UserException(UserExceptionCode.USER_CAR_ATTESTATION_NAME);
//        }
//        if (!form.getCarContractsTek().equals(userInfo.getUserInfoTel())) {
//            throw new UserException(UserExceptionCode.USER_CAR_ATTESTATION_PHONE);
//        }
        UserCar car = BeanUtil.toBean(form, UserCar.class);
        car.setUserInfoId(userInfo.getUserInfoId());
        car.setUserCarState(StateData.USER_CAR_STATE);
        userCarMapper.insert(car);


    }
    /**
     * 修改用户车辆信息
     * 参数：
     * userCarPlate：车牌号
     * userCarBrand：车牌品牌
     * carContacts：车辆信息
     * carContractsTek：车辆联系人电话
     * carType：车辆类型
//     * */
//    @Override
//    public void upUserCarData(UpUserCarForm form, Integer userId) {
//        System.out.println(form+"修改参数");
//        UserAccount account = userAccountMapper.selectById(userId);
//        if (account==null) {
//            throw new UserException(UserExceptionCode.USERINFO_IS_NOT);
//        }
//        if (account.getUserState()!= StateData.USER_STATE_ENABLE) {
//            throw new UserException(UserExceptionCode.USER_ACCOUNT_STATE_ISNOT);
//        }
//        UserCar userCar = userCarMapper.selectById(form.getUserCarId());
//        userCar.setUserCarBrand(form.getUserCarBrand());
//        userCar.setCarContacts(form.getCarContacts());
//        userCar.setCarContractsTek(form.getCarContractsTek());
//        userCar.setCarType(form.getCarType());
//        userCar.setUserCarInfo(form.getUserCarInfo());
//        System.out.println(userCar+"c参数");
//        userCarMapper.updateById(userCar);
//    }
    /**
     * 删除用户车辆信息
     * 参数：
     * 用户车辆Id:userCarId
     * */
    @Override
    public void delUserCarData(Integer userCarId, Integer userId) throws Exception {
        UserCar userCar = userCarMapper.selectById(userCarId);
        Result<List<UserOrderDto>> listResult = orderClient.selectUserOrderByUserCarId(userCarId);
        if(listResult.getCode()!=200) throw new Exception("服务器繁忙");
        List<UserOrderDto> data = listResult.getData();
        if(data!=null&&data.size()>1){
            throw new UserException(UserExceptionCode.USER_CAR_ORDER_HAVE);
        }
        userCar.setUserCarState(StateData.USER_CAR_STATE_DELETE);
        userCarMapper.updateById(userCar);
    }
    /**
     * 当前用户全部车辆信息
     * 参数：
     * authorization:令牌
     *
     * */
    @Override
    public List<UserCarDtos> findUserCarById(Integer userId) {
        QueryWrapper<UserInfo> wrapper = new QueryWrapper<>();
        wrapper.eq("user_account_id", userId);
        UserInfo userInfo = userInfoMapper.selectOne(wrapper);
        if (userInfo==null) {
            throw new UserException(UserExceptionCode.USERINFO_IS_NOT);
        }
        QueryWrapper<UserCar> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_info_id",userInfo.getUserInfoId());
        List<UserCar> userCars = userCarMapper.selectList(queryWrapper);
        List<UserCarDtos> dtos = new ArrayList<>();
        for (UserCar userCar : userCars) {
            UserCarDtos dto = BeanUtil.toBean(userCar, UserCarDtos.class);
            dto.setCarTypeName(userCar.getCarType()==1?"新能源汽车":"汽车");
            dtos.add(dto);
        }
        return dtos;
    }
    /**
     * 根据Id查询用户车辆
     * 参数: 用户车辆Id userCarId
     * 返回: 车牌号 userCarPlate
     * 车辆联系人 carContacts
     * 车辆联系人电话 carContractsTek
     * 用户信息Id    userInfoId
     * 方法: 1.判断userCarId是否存在对应的用户车辆信息 若不存在则抛异常 若被删除也抛异常
     * 3.将用户车辆放入Dto返回
     */
    @Override
    public UserCar getUserCatById(Integer userCarId) {
        return userCarMapper.selectById(userCarId);
    }

}
